///These series of functions will accept KML Geometry Features, and return either GeoJSON or ESRI JavaScript.
///The intended purpose is to allow the passing of KML Geometry objects to Geoprocessing or non Google visualization systems. 

///Make sure you care calling the ESRI JS API in your source document so we can make the ESRI JS Geometry Objects




var return_geometry = [];
//First, a function which will accept a KML Geometry, and what type of object you would like to have returned

function KMLtoGeom(geomety, return_type){

ProcessGeom(geom, return_type);

return return_geometry;

}

function ProcessGeom(geometry, return_type){



//Determine what type of geometry we have been passed, as they all will be handled differently
confirm(geometry.getType());
	if(geometry.getType() == "KmlMultiGeometry"){
		if (geometry.getGeometries().hasChildNodes()) {
			list = geometry.getGeometries().getChildNodes();
			for (i = 0; i < list.getLength(); i++) {
				geom = list.item(i);
				ProcessGeom(geom, return_type);
				
			}
		}

	} else if(geometry.getType() == "KmlPolygon"){
		PolygontoGeom(geometry, return_type);
	} else if(geometry.getType() == "KmlLineString"){
		LineStringtoGeom(geometry, return_type);
	} else if(geometry.getType() == "KmlLinearRing"){
		LinearRingtoGeom(geometry, return_type);
	} else if(geometry.getType() == "KmlPoint"){
		PointtoGeom(geometry, return_type);
	} else if(geometry.getType() == "KmlModel"){


} 



}



function PolygontoGeom(geometry, return_type){

    
    var OuterBoundary = geometry.getOuterBoundary();
    var OuterBoundaryCoords = OuterBoundary.getCoordinates();
    
    var coordx = null;
    var coordy = null;
    var ringPoints = [];
    var ring = '[';
    var counter = 0;
    if(return_type == "ESRI"){
    
    confirm(OuterBoundaryCoords.getLength());
    
    	for (i = 0; i <= OuterBoundaryCoords.getLength(); i++) {
    			
			coordx = OuterBoundaryCoords.get(i).getLongitude(); 
			coordy = OuterBoundaryCoords.get(i).getLatitude();
			ringPoints[i] = new esri.geometry.Point(coordx, coordy, new esri.SpatialReference({ wkid: 4326 }));
			
			
	 		  createplacemark(coordx,coordy,counter);
	   			counter++; 

    	}
    	confirm(i)
;	
	var ESRIGeom = new esri.geometry.Polygon(new esri.SpatialReference({ wkid: 4326 }));

	ESRIGeom.addRing(ringPoints);
	//confirm(ESRIGeom.isClockwise(ring[0])
	
	//confirm(ESRIGeom.getExtent().xmax);
	// OK, got the outside Ring, now we need to see if there are any interior rings (holes) that need to be added as well. 
	
	if (geometry.getInnerBoundaries().hasChildNodes()) {
	//confirm("hey, we have inner rings here")
			list = geometry.getInnerBoundaries().getChildNodes();
			for (i = 0; i < list.getLength(); i++) {
			//confirm(list.getLength() + "rings found"); 
				var InnerBoundary = geometry.getInnerBoundary(i);
    			var InnerBoundaryCoords = InnerBoundary.getCoordinates();
    			ringPoints = [];
    				for (j = 0; j < OuterBoundaryCoords.getLength(); j++) {
    			
						coordx = OuterBoundaryCoords.get(j).getLongitude(); 
						coordy = OuterBoundaryCoords.get(j).getLatitude();
						ringPoints[j] = new esri.geometry.Point(coordx, coordy, new esri.SpatialReference({ wkid: 4326 }));

    				}
    			ESRIGeom.addRing(ringPoints);

			}
		}
	//oK, now add our ESRI geometry to the return_geometry array
	return_geometry.push(ESRIGeom);
	
	}else if(return_type == "GeoJSON"){
	
	
	
	}

	
}

function LinearRingtoGeom(geometry, return_type){

	
    var LinearRingCoords = geometry.getCoordinates();
    
    var coordx = null;
    var coordy = null;
    var ringPoints = [];
    var ring = '[';
    
    if(return_type == "ESRI"){
    
    	for (i = 0; i < LinearRingCoords.getLength(); i++) {
    			
			coordx = LinearRingCoords.get(i).getLongitude(); 
			coordy = LinearRingCoords.get(i).getLatitude();
			ringPoints[i] = new esri.geometry.Point(coordx, coordy, new esri.SpatialReference({ wkid: 4326 }));

    	}
	
	var ESRIGeom = new esri.geometry.Polygon(new esri.SpatialReference({ wkid: 4326 }));

	ESRIGeom.addRing(ringPoints);
	

	//OK, now add our ESRI geometry to the return_geometry array
	return_geometry.push(ESRIGeom);


	}else if(return_type == "GeoJSON"){
	
	
	
	}
}


function LineStringtoGeom(geometry, return_type){


}

function PointtoGeom(geometry, return_type){


}